import {
	defineStore
} from 'pinia';


export const useIndexStore = defineStore('index', () => {
	// 视频数据
	let videoMysql = [{
			id: 1,
			nickname: '小白天下无敌',
			username: "xb666888",
			src: 'https://yy.dx1126.cn/images/tou.png',
			videoSrc: 'https://yy.dx1126.cn/video/1.mp4',
			content: `婉儿上天`,
			like: 1,
			collect: 1,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true,
			myVideoShow: false
		},
		{
			id: 2,
			nickname: '琴酒',
			username: "qj666888",
			src: 'https://yy.dx1126.cn/images/tou.png',
			videoSrc: 'https://yy.dx1126.cn/video/2.mp4',
			content: `等虎多时`,
			like: 1,
			collect: 1,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true,
			myVideoShow: false
		},
		{
			id: 3,
			nickname: '平生',
			username: "ps666888",
			src: 'https://yy.dx1126.cn/images/tou.png',
			videoSrc: 'https://yy.dx1126.cn/video/3.mp4',
			content: `婉儿精彩时刻`,
			like: 1,
			collect: 1,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true,
			myVideoShow: false
		},
		{
			id: 4,
			nickname: '小白玩游戏',
			username: "yx666888",
			src: 'https://yy.dx1126.cn/images/tou2.png',
			videoSrc: 'https://yy.dx1126.cn/video/4.mp4',
			content: `我的王者`,
			like: 1,
			collect: 1,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true,
			myVideoShow: false
		},
		{
			id: 5,
			nickname: '娱乐综艺',
			username: "yl666888",
			src: 'https://yy.dx1126.cn/images/tou.png',
			videoSrc: 'https://yy.dx1126.cn/video/5.mp4',
			content: `终于上榜了`,
			like: 1,
			collect: 1,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true,
			myVideoShow: false
		},
		{
			id: 6,
			nickname: '小白玩游戏',
			username: "yx666888",
			src: 'https://yy.dx1126.cn/images/tou2.png',
			videoSrc: 'https://yy.dx1126.cn/video/6.mp4',
			content: `市级婉儿的高端操作`,
			like: 1,
			collect: 1,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true,
			myVideoShow: false
		},
		{
			id: 7,
			nickname: '小白玩游戏',
			username: "yx666888",
			src: 'https://yy.dx1126.cn/images/tou2.png',
			videoSrc: 'https://yy.dx1126.cn/video/7.mp4',
			content: `老夫子无敌`,
			like: 1,
			collect: 1,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true,
			myVideoShow: false
		},
		{
			id: 8,
			nickname: '小白玩游戏',
			username: "yx666888",
			src: 'https://yy.dx1126.cn/images/tou2.png',
			videoSrc: 'https://yy.dx1126.cn/video/8.mp4',
			content: `老虎的精彩时刻`,
			like: 1,
			collect: 1,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true,
			myVideoShow: false
		},
		{
			id: 9,
			nickname: '小白看电视',
			username: "ds666888",
			src: 'https://yy.dx1126.cn/images/tou2.png',
			videoSrc: 'https://yy.dx1126.cn/video/9.mp4',
			content: `婉儿捡漏王`,
			like: 1,
			collect: 1,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true,
			myVideoShow: false
		}
	]
	// 音乐数据
	let mudios = [{
			id: 1,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue1.mp3",
			title: '天外来物',
			singer: '薛之谦'
		},
		{
			id: 2,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue2.mp3",
			title: '其实',
			singer: '薛之谦'
		},
		{
			id: 3,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue3.mp3",
			title: '那是你离开了北京的生活',
			singer: '薛之谦'
		},
		{
			id: 4,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue4.mp3",
			title: '我好像在哪见过你',
			singer: '薛之谦'
		},
		{
			id: 5,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue5.mp3",
			title: '演员',
			singer: '薛之谦'
		},
		{
			id: 6,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue6.mp3",
			title: '陪你去流浪',
			singer: '薛之谦'
		},
		{
			id: 7,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue7.mp3",
			title: '刚刚好',
			singer: '薛之谦'
		},
		{
			id: 8,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue8.mp3",
			title: '怪咖',
			singer: '薛之谦'
		},
		{
			id: 9,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue9.mp3",
			title: '绅士',
			singer: '薛之谦'
		},
		{
			id: 10,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue10.mp3",
			title: '动物世界',
			singer: '薛之谦'
		},
		{
			id: 11,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue11.mp3",
			title: '像风一样',
			singer: '薛之谦'
		},
		{
			id: 12,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue12.mp3",
			title: '暧昧',
			singer: '薛之谦'
		},
		{
			id: 13,
			src: 'https://yy.dx1126.cn/images/xue1.jpg',
			url: "https://yy.dx1126.cn/audio/xue13.mp3",
			title: '认真的雪',
			singer: '薛之谦'
		},
	]
	// 关注
	let guanZhu = []
	// 关注信息字段
	// {
	// 	id: 1,
	// 	username: "xb666888",
	// 	token: 'yx666888',
	// 	beiSrc: 'https://yy.dx1126.cn/images/myBei1.jpg',
	// 	src: 'https://yy.dx1126.cn/images/tou2.png',
	// 	password: '123456',
	// 	nickname: '小白天下无敌',
	// 	fenSi: 0,
	// 	guanZhu: 0,
	// 	like: 0,
	// 	dsc: '点赞关注我的，都是帅哥美女'
	// }

	// 全部个人信息
	let userItems = [{
			id: 1,
			username: "yx666888",
			beiSrc: 'https://yy.dx1126.cn/images/myBei1.jpg',
			src: 'https://yy.dx1126.cn/images/tou2.png',
			password: '123456',
			nickname: '小白玩游戏',
			fenSi: 0,
			guanZhu: 0,
			like: 0,
			dsc: '点赞关注我的，都是帅哥美女'
		},
		{
			id: 2,
			username: "xb666888",
			beiSrc: 'https://yy.dx1126.cn/images/myBei1.jpg',
			src: 'https://yy.dx1126.cn/images/tou2.png',
			password: '123456',
			nickname: '小白天下无敌',
			fenSi: 0,
			guanZhu: 0,
			like: 0,
			dsc: '点赞关注我的，都是帅哥美女'
		},
		{
			id: 3,
			username: "qj666888",
			beiSrc: 'https://yy.dx1126.cn/images/myBei1.jpg',
			src: 'https://yy.dx1126.cn/images/tou2.png',
			password: '123456',
			nickname: '琴酒',
			fenSi: 0,
			guanZhu: 0,
			like: 0,
			dsc: '点赞关注我的，都是帅哥美女'
		},
		{
			id: 4,
			username: "ps666888",
			beiSrc: 'https://yy.dx1126.cn/images/myBei1.jpg',
			src: 'https://yy.dx1126.cn/images/tou2.png',
			password: '123456',
			nickname: '平生',
			fenSi: 0,
			guanZhu: 0,
			like: 0,
			dsc: '点赞关注我的，都是帅哥美女'
		},
		{
			id: 5,
			username: "yl666888",
			beiSrc: 'https://yy.dx1126.cn/images/myBei1.jpg',
			src: 'https://yy.dx1126.cn/images/tou2.png',
			password: '123456',
			nickname: '娱乐综艺',
			fenSi: 0,
			guanZhu: 0,
			like: 0,
			dsc: '点赞关注我的，都是帅哥美女'
		},
		{
			id: 6,
			username: "ds666888",
			beiSrc: 'https://yy.dx1126.cn/images/myBei1.jpg',
			src: 'https://yy.dx1126.cn/images/tou2.png',
			password: '123456',
			nickname: '小白看电视',
			fenSi: 0,
			guanZhu: 0,
			like: 0,
			dsc: '点赞关注我的，都是帅哥美女'
		},
	]
	// 个人信息初始化
	userItems[0].guanZhu = guanZhu.filter(item => item.token == userItem.username).length
	// 个人信息
	let userItem = userItems[0]

	// 评论
	let pings = [{
		id: 1,
		nickname: '小美',
		src: 'https://yy.dx1126.cn/images/tou2.png',
		content: `小姐姐太漂亮了`,
		token: 'https://yy.dx1126.cn/video/7.mp4'
	}]

	// 赞
	let likeList = []
	// 收藏
	let shouList = []


	// 视频缓存
	const setStorageV = () => {
		try {
			uni.setStorageSync('video', videoMysql);
		} catch (err) {
			console.log('缓存失败：', err);
		}
	}
	// 点赞缓存
	const setStorageL = () => {
		try {
			uni.setStorageSync('like', likeList);
		} catch (err) {
			console.log('缓存失败：', err);
		}
	}
	// 收藏缓存
	const setStorageS = () => {
		try {
			uni.setStorageSync('shou', shouList);
		} catch (err) {
			console.log('缓存失败：', err);
		}
	}
	// 关注缓存
	const setStorageG = () => {
		try {
			uni.setStorageSync('guan', guanZhu);
		} catch (err) {
			console.log('缓存失败：', err);
		}
	}
	// 评论缓存
	const setStorageP = (obj) => {
		try {
			pings.unshift(obj)
			uni.setStorageSync('ping', pings);
		} catch (err) {
			console.log('缓存失败：', err);
		}
	}
	// 个人信息和全部账号信息更改背景缓存
	const setStorageU = (src, username) => {
		try {
			if (userItem.username == username) {
				userItem.beiSrc = src
			}
			userItems.forEach(item => {
				if (item.username == username) {
					item.beiSrc = src
				}
			})
			uni.setStorageSync('user', userItem);
			uni.setStorageSync('users', userItems);
		} catch (err) {
			console.log('缓存失败：', err);
		}
	}
	// 更换头像  
	const setStorageTou = (src, username) => {
		try {
			if (userItem.username == username) {
				userItem.src = src
			}
			userItems.forEach(item => {
				if (item.username == username) {
					item.src = src
				}
			})
			videoMysql.forEach(itemV => {
				if (itemV.username == username) {
					itemV.src = src
				}
			})
			setStorageV()
			uni.setStorageSync('user', userItem);
			uni.setStorageSync('users', userItems);
		} catch (err) {
			console.log('缓存失败：', err);
		}
	}


	// 读取所有缓存
	const getStorage = () => {
		try {
			// 评论
			const valueP = uni.getStorageSync('ping');
			if (valueP) {
				pings = valueP
			}
			// 点赞
			const valueL = uni.getStorageSync('like');
			if (valueL) {
				likeList = valueL
			}
			// 收藏
			const valueS = uni.getStorageSync('shou');
			if (valueS) {
				shouList = valueS
			}
			// 个人信息
			const valueU = uni.getStorageSync('user');
			if (valueU) {
				userItem = valueU
			}
			// 全部信息
			const valueUS = uni.getStorageSync('users');
			if (valueUS) {
				userItems = valueUS
			}
			// 视频
			const valueV = uni.getStorageSync('video');
			if (valueV) {
				videoMysql = valueV
				// 头像赋值
				userItems.forEach(itemU => {
					videoMysql.forEach(itemV => {
						if (itemU.username == itemV.username) {
							itemV.src = itemU.src
						}
					})
				})
				// 点赞赋值videoMysql
				videoMysql.forEach(itemV => {
					likeList.forEach(itemL => {
						if (itemL.videoSrc == itemV.videoSrc) {
							itemV.like = itemL.like
							itemV.likeShow = false
						}
					})
				})
				// 收藏赋值
				videoMysql.forEach(itemV => {
					shouList.forEach(itemS => {
						if (itemS.videoSrc == itemV.videoSrc) {
							itemV.collect = itemS.collect
							itemV.collectShow = false
						}
					})
				})
				setStorageV()
			}
			// 关注
			const valueG = uni.getStorageSync('guan');
			userItem.guanZhu = guanZhu.filter(item => item.token == userItem.username).length
			if (valueG) {
				guanZhu = valueG
				userItem.guanZhu = guanZhu.filter(item => item.token == userItem.username).length
				if (userItem.guanZhu == 0) {
					videoMysql.forEach(itemV => {
						itemV.guanShow = true
					})
				} else {
					guanZhu.forEach(itemG => {
						if (itemG.token == userItem.username) {
							videoMysql.forEach(itemV => {
								if (itemG.username == itemV.username) {
									itemV.guanShow = false
								}
							})
						}
					})
				}
				setStorageV()
			} else {
				videoMysql.forEach(itemV => {
					itemV.guanShow = true
				})
				setStorageV()
			}
			// 音频
			const valueAu = uni.getStorageSync('audio');
			if (valueAu) {
				mudios = valueAu
			}
		} catch (e) {
			console.log('读取失败：', e);
		}
	}
	getStorage()

	// 增加关注
	let addGuan = (username) => {
		guanZhu.forEach((item, index) => {
			if (item.username == username) {
				guanZhu.splice(index, 1)
			}
		})
		userItems.forEach((item, index) => {
			if (item.username == username) {
				let obj = JSON.parse(JSON.stringify(item))
				obj.token = userItem.username
				guanZhu.push(obj)
			}
		})
		userItem.guanZhu = guanZhu.filter(item => item.token == userItem.username).length
		userItems.forEach(item => {
			if (item.username == userItem.username) {
				item.guanZhu = guanZhu.filter(item => item.token == userItem.username).length
			}
		})
		try {
			uni.setStorageSync('user', userItem);
			uni.setStorageSync('users', userItems);
		} catch (err) {
			console.log('缓存失败：', err);
		}
		setStorageG()
		setStorageV()
	}

	// 取消关注
	let removeGuan = (username) => {
		guanZhu.forEach((item, index) => {
			if (item.username == username) {
				guanZhu.splice(index, 1)
			}
		})
		// userItems.forEach((item, index) => {
		// 	if (item.username == username) {
		// 		let obj = JSON.parse(JSON.stringify(item))
		// 		obj.token = userItem.username
		// 		guanZhu.push(obj)
		// 	}
		// })
		userItem.guanZhu = guanZhu.filter(item => item.token == userItem.username).length
		userItems.forEach(item => {
			if (item.username == userItem.username) {
				item.guanZhu = guanZhu.filter(item => item.token == userItem.username).length
			}
		})
		try {
			uni.setStorageSync('user', userItem);
			uni.setStorageSync('users', userItems);
		} catch (err) {
			console.log('缓存失败：', err);
		}
		setStorageG()
		setStorageV()
	}

	// 视频发布
	const addVideo = (videoSrc, text) => {
		let id = videoMysql.length + Math.floor(Math.random() * 10000 + 1)
		let obj = {
			id,
			nickname: userItem.nickname,
			username: userItem.username,
			src: userItem.src,
			videoSrc: videoSrc,
			content: text,
			like: 0,
			collect: 0,
			loadShow: false,
			zanShow: true,
			pingShow: false,
			guanShow: true,
			likeShow: true,
			collectShow: true
		}
		videoMysql.unshift(obj)
		// 数据缓存
		setStorageV()
	}

	// 删除视频
	const removeVideo = (username, videoSrc) => {
		videoMysql.forEach((item, index) => {
			if (item.username == username) {
				if (item.videoSrc == videoSrc) {
					videoMysql.splice(index, 1)
				}
			}
		})
		likeList.forEach((item, index) => {
			if (item.videoSrc == videoSrc) {
				likeList.splice(index, 1)
			}
		})
		shouList.forEach((item, index) => {
			if (item.videoSrc == videoSrc) {
				shouList.splice(index, 1)
			}
		})
		uni.showToast({
			title: '已删除',
			duration: 2000
		});
		// 数据缓存
		setStorageV()
		setStorageL()
		setStorageS()
	}

	// 视频随机播放
	const randomVideo = () => {
		let arrIndex = []
		for (let i = 0; i < Math.ceil(videoMysql.length / 2); i++) {
			let random = Math.floor(Math.random() * (videoMysql.length + 1))
			arrIndex.push(random)
			arrIndex.forEach((item, index) => {
				if (arrIndex.indexOf(item) != index) {
					arrIndex.splice(index, 1)
				}
			})
		}
		videoMysql.forEach((item, index) => {
			arrIndex.forEach(items => {
				if (items == index) {
					videoMysql.splice(index, 1)
					videoMysql.unshift(item)
				}
			})
		})
	}
	randomVideo()

	// 点赞
	const setLike = (videoSrc) => {
		videoMysql.forEach((item, index) => {
			if (item.videoSrc == videoSrc) {
				let obj = JSON.parse(JSON.stringify(item))
				obj.token = userItem.username
				likeList.push(obj)
			}
		})
		// 数据缓存
		setStorageV()
		setStorageL()
	}
	// 取消点赞
	const removeLike = (videoSrc, role) => {
		likeList.forEach((item, index) => {
			if (item.videoSrc == videoSrc) {
				likeList.splice(index, 1)
			}
		})
		// 判断是首页取消则不执行以下方法
		if (role == 0) {
			// 数据缓存
			setStorageV()
			setStorageL()
			return
		}
		videoMysql.forEach((item, index) => {
			if (item.videoSrc == videoSrc) {
				item.likeShow = !item.likeShow
				if (item.likeShow) {
					item.like--
				} else {
					item.like++
				}
			}
		})
		// 数据缓存
		setStorageV()
		setStorageL()
	}
	// 收藏
	const setShou = (videoSrc) => {
		videoMysql.forEach((item, index) => {
			if (item.videoSrc == videoSrc) {
				let obj = JSON.parse(JSON.stringify(item))
				obj.token = userItem.username
				shouList.push(obj)
			}
		})
		// 数据缓存
		setStorageV()
		setStorageS()
	}
	// 取消收藏
	const removeShou = (videoSrc, role) => {
		shouList.forEach((item, index) => {
			if (item.videoSrc == videoSrc) {
				shouList.splice(index, 1)
			}
		})
		// 判断是首页取消则不执行以下方法
		if (role == 0) {
			// 数据缓存
			setStorageV()
			setStorageS()
			return
		}
		videoMysql.forEach((item, index) => {
			if (item.videoSrc == videoSrc) {
				item.collectShow = !item.collectShow
				if (item.collectShow) {
					item.collect--
				} else {
					item.collect++
				}
			}
		})
		// 数据缓存
		setStorageV()
		setStorageS()
	}

	return {
		videoMysql,
		mudios,
		addVideo,
		userItem,
		removeVideo,
		randomVideo,
		likeList,
		setLike,
		shouList,
		setShou,
		removeLike,
		removeShou,
		addGuan,
		setStorageV,
		setStorageP,
		pings,
		setStorageU,
		setStorageTou,
		userItems,
		guanZhu,
		removeGuan
	}
});